import { http, HttpResponse } from "msw";
import { getManageList, getProductDetailByID, shelfOff ,operateForProduct, manageTableList,Sale_List} from "./assets";
import { StatisticsApi } from "@/api/service/manageService";

// manageTabelList接口
const getManageTableList = http.post(
  "/api/manage/getManageTableList",
  async ({ request, params, cookies }) => {
    const data = await request.json();

    return HttpResponse.json({
      message: "数据请求成功！",
      data: getManageList(data),
      status: 200,
    });
  }
);

const getProductByID = http.get(
  "/api/manage/getProductByID",
  async ({ request, params, cookies }) => {
    const query = new URL(request.url).searchParams;
    const id = query.get("id");
    console.log(id, getProductDetailByID(id));
    return HttpResponse.json({
      message: "数据请求成功！",
      data: getProductDetailByID(id),
      status: 200,
    });
  }
);

const shelf_off = http.get(
  "/api/manage/shelfOff",
  async ({ request, params, cookies }) => {
    const query = new URL(request.url).searchParams;
    const id = query.get("id");
    shelfOff(id);
    return HttpResponse.json({
      message: "商品下架成功！",
      data: null,
      status: 200,
    });
  }
);

const doOperateForProduct=http.put(
  "/api/manage/doOPerate",
  async ({ request, params, cookies }) => {
    const data = await request.json();
    operateForProduct(data);
    return  HttpResponse.json({
      message: "操作记录成功！",
      data: null,
      status: 200,
    });
  }
)

//获取指定时间段内兑换量
const exchangeVolume = http.post(
  `/api${StatisticsApi.ExchangeVolume}`,
  async ({ request, params, cookies }) => {
    const { startDate: start, endDate: end } = await request.json();
    // 转换为Date对象
    const startDate = new Date(start);
    const endDate = new Date(end);
    // 筛选在指定时间段内的销售数据
    const filteredData = Sale_List.filter(item => {
      const saleDate = new Date(item.sale_time);
      return saleDate >= startDate && saleDate <= endDate;
    });

    // 按日期统计兑换量
    const exchangeData = {};
    filteredData.forEach(item => {
      const date = item.sale_time.split('T')[0];
      if (exchangeData[date]) {
        exchangeData[date] += item.count;
      } else {
        exchangeData[date] = item.count;
      }
    });

    const datas = Object.keys(exchangeData).map(date => ({
      date: date,
      total: exchangeData[date]
    }));

    if(datas.length===0){
      return HttpResponse.json({
        message: "没有查询到数据!",
        status: 10003,
      });
    }

    return HttpResponse.json({
      status: 200,
      message: "数据请求成功！",
      data: {
        datas
      },
    });
  }
);

// 指定时间段内 top20 品类
const topCategories = http.post(
  `/api${StatisticsApi.TopCategories}`,
  async ({ request, params, cookies }) => {
    const { startDate: start, endDate: end } = await request.json();
    // 转换为Date对象
    const startDate = new Date(start);
    const endDate = new Date(end);
    // 筛选在指定时间段内的销售数据
    const filteredData = Sale_List.filter(item => {
      const saleDate = new Date(item.sale_time);
      return saleDate >= startDate && saleDate <= endDate;
    });
    // 统计品类销售数量
    const categoryData = {};
    filteredData.forEach(item => {
      const category = item.goods_category; 
      if (categoryData[category]) {
        categoryData[category] += item.count;
      } else {
        categoryData[category] = item.count;
      }
    });
    const sortedCategories = Object.keys(categoryData).map(category => ({
      category: category,
      total: categoryData[category]
    })).sort((a, b) => b.total - a.total); 
    // 取前20个品类
    const datas = sortedCategories.slice(0, 20);
    if (datas.length === 0) {
      return HttpResponse.json({
        message: "没有查询到数据!",
        status: 10003,
      });
    }
    return HttpResponse.json({
      status: 200,
      message: "数据请求成功！",
      data: {
        datas
      } 
    });
  }
);


//指定时间段内兑换方式总数
const exchangeMethods = http.post(
  `/api${StatisticsApi.ExchangeMethods}`, 
  async ({ request, params, cookies }) => {
    const { startDate: start, endDate: end } = await request.json();
    // 转换为Date对象
    const startDate = new Date(start);
    const endDate = new Date(end);
    
    const filteredData = Sale_List.filter(item => {
      const saleDate = new Date(item.sale_time);
      return saleDate >= startDate && saleDate <= endDate;
    });
    // 统计兑换方式数量
    const methodData = {};
    filteredData.forEach(item => {
      const method = item.pay_type; 
      if (methodData[method]) {
        methodData[method] += item.count;
      } else {
        methodData[method] = item.count;
      }
    });
    const datas = Object.keys(methodData).map(method => ({
      method: method,
      total: methodData[method]
    }));
    if (datas.length === 0) {
      return HttpResponse.json({
        message: "没有查询到数据!",
        status: 10003,
      });
    }
    return HttpResponse.json({
      status: 200,
      message: "数据请求成功！",
      data: {
        datas
      } 
    });
  }
);

const ManageAPI = [getManageTableList, getProductByID,shelf_off,doOperateForProduct,exchangeVolume,topCategories,exchangeMethods];
export default ManageAPI;
